﻿using Qiniu.IO;
using Qiniu.IO.Resumable;
using Qiniu.RS;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WeixinImageDownLoadServer
{
    public class QiniuUploadTask
    {
        private Stream inputStream = null;
        private string imagePath = "";
        private string pid = "";
        private object tag = null;
        private long filesize = 0;
        private string fileExt = "";
        public QiniuUploadTask(string imagePath, Stream inputStream, string pid)
        {
            this.imagePath = imagePath;
            this.inputStream = inputStream;
            this.pid = pid;
        }

        public QiniuUploadTask(string imagePath, Stream inputStream, object tag, string pid)
        {
            this.imagePath = imagePath;
            this.inputStream = inputStream;
            this.tag = tag;
            this.pid = pid;
        }

        private bool _stop { get; set; }

        public void Stop()
        {
            _stop = true;
        }

        public QiniuUploadResult Run(object stat)
        {
            QiniuUploadResult uploadResult = null;
            if (_stop) return null;

            try
            {
                Qiniu.Conf.Config.ACCESS_KEY = "rN2Wx9usqx7K8EDQSkga1IQV1jNuP5uI0mYbFOEx";
                Qiniu.Conf.Config.SECRET_KEY = "mcuuFvsXSucmdhv7z2Fznwbi6iTpnKDvQhBKx7As";
                string spanceurl = "7xj84r.com2.z0.glb.qiniucdn.com";
                string bucketName = "muzhichun";
                PutPolicy policy = new PutPolicy(bucketName, 3600);
                string upToken = policy.Token();
                Settings setting = new Settings();
                ResumablePutExtra extra = new ResumablePutExtra();
                //ResumablePut client = new ResumablePut(setting, extra);
                //var result = client.PutFile(upToken, imagePath, Guid.NewGuid().ToString());
                IOClient client = new IOClient();
                var result = client.Put(upToken, imagePath, inputStream, new PutExtra());
                if (result.OK)
                {
                    string key = GetMidString(result.Response, "\"key\":\"", "\"");
                    string imgurl = "http://" + spanceurl + "/" + key;
                    uploadResult = new QiniuUploadResult() { success = true, url = imgurl, imagepath = imagePath, tag = tag, pid = pid };
                    AppReportManager.Instance.Send<QiniuUploadResult>(new QiniuUploadResult() { success = true, pid = pid, url = imgurl, imagepath = imagePath, tag = tag });
                }
                else
                {
                    uploadResult = new QiniuUploadResult() { success = false, msg = result.Exception.Message, url = result.Response, imagepath = imagePath, tag = tag, pid = pid };
                    AppReportManager.Instance.Send<QiniuUploadResult>(new QiniuUploadResult() { success = false, msg = result.Exception.Message, pid = pid, url = result.Response, imagepath = imagePath, tag = tag });
                }
            }
            catch (Exception ex)
            {
                uploadResult = new QiniuUploadResult() { msg = "上传出错:" + ex.Message, success = false };
                AppReportManager.Instance.Send<QiniuUploadResult>(new QiniuUploadResult() { msg = "上传出错:" + ex.Message, success = false });
            }
            return uploadResult;
        }

        public string GetMidString(string text, string leftStirng, string rightString)
        {
            if (string.IsNullOrEmpty(text)) return "";
            int pos1 = text.IndexOf(leftStirng);
            if (pos1 > -1)
            {
                pos1 = pos1 + leftStirng.Length;
                int pos2 = text.IndexOf(rightString, pos1);
                if (pos2 == -1) return "";
                return text.Substring(pos1, pos2 - pos1);
            }
            else
            {
                return "";
            }
        }

    }

    public class QiniuUploadResult : System.EventArgs
    {
        public bool success { get; set; }
        public string pid { get; set; }
        public string imagepath { get; set; }
        public string url { get; set; }
        public string msg { get; set; }
        public object tag { get; set; }
    }

    public class QiniuUploadLog : System.EventArgs
    {
        public string imagepath { get; set; }
        public string msg { get; set; }
    }
}
